﻿#ifndef SHELLAUDIO_H
#define SHELLAUDIO_H
#include "qcontainerfwd.h"
#include <QVariantList>
#include <QMutex>
#include <MediaPlayer.h>
#include <QReadWriteLock>
class ShellAudio:public QObject
{
    Q_OBJECT
public:
    static ShellAudio* getInstance(){
        static QMutex mutex;
        if (!instance) {
            QMutexLocker locker(&mutex); // 线程安全
            if (!instance) {
                instance.reset(new ShellAudio);
            }
        }
        return instance.data();
    }
    ~ShellAudio() = default;
    static void init();
    void initClientId();
    Q_INVOKABLE QVariantList getAudioInputDevices();
    Q_INVOKABLE void createAudioRecord(QString taskId, int sessionId,
                                       QString device
                                       , int bitrate, MediaPlayer::CH_MODE chMode);
    Q_INVOKABLE void startAudioRecord(QString taskId);
    Q_INVOKABLE void stopAudioRecord(QString taskId);
    Q_INVOKABLE void pauseAudioRecord(QString taskId);
    Q_INVOKABLE void stopAll();
    Q_INVOKABLE void addLocalTask(QString userName, QString taskId);
    Q_INVOKABLE QVariantList getLocalTask(QString userName);
    Q_INVOKABLE QString getClientId();
    Q_INVOKABLE void setUseFFmpeg(bool useFFmpeg);
    Q_INVOKABLE bool getUseFFmpeg();
protected slots:
signals:
    void playerStateChanged(MediaPlayer::PLAYER_STATE playerState,QString taskId,int sessionId,QString msg);
private:
    ShellAudio(QObject* parent = nullptr):QObject(parent){
        qDebug()<<"SellAudio Constructed";
    }

    Q_DISABLE_COPY(ShellAudio)

    static QScopedPointer<ShellAudio> instance;

    QVariantList _audioInputDevices;
    void printfHex(const char * str);
    QMap<QString,MediaPlayer*> _mediaPlayerMap;
    bool _useFFmpeg;
};

#endif // SHELLAUDIO_H
